/*
 * 文 件    名:  SearchHistoryImpl.java
 * 创建日期:  2012-10-10下午2:59:24
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  cl073
 */
package com.weipu.common.facade.content.impl;

import java.util.Arrays;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;
import android.provider.BaseColumns;

import com.weipu.common.facade.content.BaseContentProvider;
import com.weipu.common.facade.content.ContentURI;
import com.weipu.common.facade.model.HomeModuleModel;
import com.weipu.common.facade.model.ModuleChildrenModel;
import com.weipu.common.util.Logger;

/** 
 * @Description 本地搜索历史记录接口
 * @version [版本号, 2012-10-10]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author cl073
 */
public class HomeDataContentProviderImpl extends BaseContentProvider
{
    /**
     * log tag
     */
    private static final String TAG = HomeDataContentProviderImpl.class.getName();
    
    /**
     * table name
     */
    private static final String TABLE_NAME = ContentURI.HomeModuleData.TABLE_NAME;
    
    /**
     * delete
     */
    public int delete(Uri uri, String selection, String[] selectionArgs)
    {
        Logger.d(TAG,
            "enter into delete, parameter {selection:" + selection + ",args:" + Arrays.toString(selectionArgs) + "}");
        int deleteRow = dbWriter.delete(TABLE_NAME, selection, selectionArgs);
        Logger.d(TAG, "exit from delete, result " + deleteRow);
        return deleteRow;
        
    }
    
    /**
     * get content type by config
     * @param uri
     * @return
     */
    public String getType(Uri uri)
    {
        return null;
    }
    
    /**
     * add
     * @param uri
     * @param values
     * @return
     */
    public Uri insert(Uri uri, ContentValues values)
    {
        Logger.d(TAG, "enter into insert");
        long rowId = dbWriter.insert(TABLE_NAME, null, values);
        values.put(BaseColumns._ID, rowId);
        Logger.d(TAG, "exit from insert, result " + rowId);
        return uri;
        
    }
    
    /**
     * query
     * @param uri
     * @param projection
     * @param selection
     * @param selectionArgs
     * @param sortOrder
     * @return
     */
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    {
        return dbReader.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
        
    }
    
    /**
     * 批量插入首页数据
     * @param hmms 数据远
     */
    public void batchInsertHomeData(List<HomeModuleModel> hmms){
//		create table tbl_home_Modulelist(_id integer primary key autoincrement, active_id text, active_name text, active_type integer, active_sort long, active_linkUrl text,active_phoneImg text,active_padImg text,active_sku integer active_linkType integer,module_name text,module_sort long,module_type integer,module_isShow integer );";
    	String sql = "INSERT INTO tbl_home_Modulelist (active_id , active_name , " +
    			"active_type , active_sort , active_linkUrl ,active_phoneImg ," +
    			"active_padImg ,active_sku , active_linkType ,module_name ," +
    			"module_sort ,module_type ,module_isShow) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    	dbWriter.beginTransaction();
    	SQLiteStatement stmt = dbWriter.compileStatement(sql);
    	
    	for (int i = 0; i < hmms.size(); i++) {
    		HomeModuleModel hmm=hmms.get(i);
    		List<ModuleChildrenModel> mcms=hmm.getChildren();
    		for (int j = 0; j < mcms.size(); j++) {
    			ModuleChildrenModel mcm=mcms.get(j);
    			 stmt.bindString(1, mcm.getActiveID()==null?"":mcm.getActiveID());
    	    	 stmt.bindString(2, mcm.getName()==null?"":mcm.getName());
    	    	 stmt.bindLong(3, mcm.getActiveType());
    	    	 stmt.bindLong(4, mcm.getSort());
    	    	 stmt.bindString(5, mcm.getLinkUrl()==null?"":mcm.getLinkUrl());
    	    	 stmt.bindString(6, mcm.getPhoneImg()==null?"":mcm.getPhoneImg());
    	    	 stmt.bindString(7, mcm.getPadImg()==null?"":mcm.getPhoneImg());
    	    	 stmt.bindLong(8, mcm.getSku());
    	    	 stmt.bindLong(9, mcm.getLinkType());
    	    	 stmt.bindString(10, hmm.getName()==null?"":hmm.getName());
    	    	 stmt.bindLong(11, hmm.getSort());
    	    	 stmt.bindLong(12, hmm.getModuleType());
    	    	 stmt.bindLong(13, hmm.isIsShow()?1:0);
    	    	 stmt.execute();
    	    	 stmt.clearBindings();
			}
//    	    stmt.bindString(1, values.get(i).number);
//    	    stmt.bindString(2, values.get(i).nick);
//    	    
    	}
    	 
    	dbWriter.setTransactionSuccessful();
    	dbWriter.endTransaction();
    }
    
}
